package compare;

import java.io.File;
import java.io.IOException;

public class ImageHandler {

	private final String fileExtension = new String("jpg");
	private File sourceImgDir;
	private File segmentationImgDir;
	private File binarycodesImgDir;
	private File softwareDir;
	
	
	public ImageHandler(File imgDir, File sourceImgDir, File segmentationImgDir, File binarycodesImgDir, File softwareDir) {
		this.sourceImgDir		= sourceImgDir;
		this.segmentationImgDir = segmentationImgDir;
		this.binarycodesImgDir 	= binarycodesImgDir;
		this.softwareDir 		= softwareDir;
	}
	
	public void readSource() throws IOException ,InterruptedException{

		File[] files = sourceImgDir.listFiles();

		for (File f : files) {
			if (f.getName().toLowerCase().endsWith(fileExtension)) {
				executeSegmentation(f);
			}
		}
	}
	
	private void executeSegmentation( File f) throws IOException, InterruptedException{
		
		String softwareCaht 	= softwareDir + "\\caht.exe";
		String softwareWahet 	= softwareDir + "\\wahet.exe";
		String input 			= " -i " + sourceImgDir + "\\"+ f.getName();
		String outputCaht 			= " -o " + segmentationImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_caht_texture.jpg -e";
		String outputWahet 			= " -o " + segmentationImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_wahet_texture.jpg -e";
		
		// run caht.exe 
		Runtime.getRuntime().exec(softwareCaht + input + outputCaht);
					
		// run wahet.exe
		Runtime.getRuntime().exec(softwareWahet + input + outputWahet);
		
		//Thread.sleep(1000);
		
	}
	
	public void readSegmentation() throws IOException {

		File[] files = segmentationImgDir.listFiles();

		for (File f : files) {
			if (f.getName().toLowerCase().endsWith(fileExtension)) {
				executeFeatureExtraction( f);
			}
		}
	}
	
	private void executeFeatureExtraction(File f) throws IOException{
		String softwarelg 	= softwareDir + "\\lg.exe";
		String softwareqsw 	= softwareDir + "\\qsw.exe";
		String softwareko 	= softwareDir + "\\ko.exe";
		String softwarecr 	= softwareDir + "\\cr.exe";
		String softwarecb 	= softwareDir + "\\cb.exe";
		String softwaredct 	= softwareDir + "\\dct.exe";
		
		String input 		= " -i " + segmentationImgDir + "\\"+ f.getName();
	
		String outputlg 			= " -o " + binarycodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_lg_code.jpg";
		String outputqsw 			= " -o " + binarycodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_qsw_code.jpg";
		String outputko				= " -o " + binarycodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_ko_code.jpg";
		String outputcr				= " -o " + binarycodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_cr_code.jpg";
		String outputcb				= " -o " + binarycodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_cd_code.jpg";
		String outputdct 			= " -o " + binarycodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_dct_code.jpg";
		
		
		// run lg.exe 
		Runtime.getRuntime().exec(softwarelg + input + outputlg);
							
		// run qsw.exe
		Runtime.getRuntime().exec(softwareqsw + input + outputqsw);
		
		// run ko.exe
		Runtime.getRuntime().exec(softwareko + input + outputko);
							
		// run cr.exe
		Runtime.getRuntime().exec(softwarecr + input + outputcr);
				
		// run cb.exe
		Runtime.getRuntime().exec(softwarecb + input + outputcb);
							
		// run dct.exe
		Runtime.getRuntime().exec(softwaredct + input + outputdct);
		
	}
	
	public File[] getBinaryCodes() {
		
		File[] files = binarycodesImgDir.listFiles();

		return files;
	}
}
